import {query,withdrawAudit} from '../services/AuditCash'
import { parse } from 'qs'
import { isEmptyObject } from '../utils'

export default {

  namespace: 'ac',

  state: {
    list:[],
    detail:[],
    DetailTags:false,
    IndexListBon:true,
    autStatus:'',
    pagination: {
      showSizeChanger: true,
      showQuickJumper: true,
      showTotal: total => `共 ${total} 条`,
      current: 1,
      total: null,
    },
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen(location => {
        if (location.pathname === '/AuditCash') {
          dispatch({
            type: 'query',
            payload: location.query,
          })
          dispatch({
            type: 'initialization',
          })
        }
      })
    },
  },
  effects: {
    *query ({ payload }, { call, put }) {
        var page = {};  
        let autStatus ='';
        if(payload.autStatus && payload.autStatus !='item_0'){
          autStatus=payload.autStatus
        }else{
          autStatus=''
        }
        if(isEmptyObject(payload.page)){
          page = {total:0,current:1,pageSize:10,field:payload.field,keyword:payload.keyword,autStatus:autStatus}
        }else{
          page = {current:payload.page,pageSize:payload.pageSize,field:payload.field,keyword:payload.keyword,autStatus:autStatus}
        }
        const data = yield call(query, parse(page))
        if (data) {
          yield put({type:'showDetail'})
          yield put({
            type: 'querySuccess',
            payload: {
              list: data.data.rows,
              pagination: {total:data.data.total,current:data.data.offset + 1,pageSize:(data.data.pageSize)},
              autStatus:autStatus
            }
          })
        }
    },
    *detail({payload},{call,put}){
        yield put({type:'showDetail'})
        yield put({
            type: 'detailsit',
            payload: {
                detail:payload,
            }
        })
    },
    *AuditCash({payload},{call,put}){
      const data = yield call(withdrawAudit,payload.data)
      if(data.data.status == 200){
        const page = {current:payload.page.current,pageSize:payload.page.pageSize,autStatus:payload.autStatus}
        const faill = yield call(query, parse(page))
        if (faill.data.status == 200) {
          yield put({
            type: 'querySuccess',
            payload: {
              list: faill.data.rows,
              detail:faill.data.rows[1],
              pagination: {total:faill.data.total,current:faill.data.offset + 1,pageSize:(faill.data.pageSize)},
            }
          })
        }
      }
    },
  },

  reducers: {
    querySuccess (state, action) {
      return { ...state,
        ... action.payload,
        pagination: {
          ...state.pagination,
          ...action.payload.pagination,
        } }
    },
    initialization(state, action){
      return {
        ...state,IndexListBon:true
      }
    },
    detailsit(state,action){
         const { detail} = action.payload
        return { ...state, detail,IndexListBon:false}
    },
    showDetail(state,action){
        return { ...state, DetailTags:true,}
    },
  },

}
